﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesLib;
using System.ComponentModel;
using myTunes.BO;

namespace myTunes.API
{
    public class iTunesAPI
    {
        public delegate void ItunesInitilizedHandler(iTunesAppClass iTunes);
        public delegate void NewTrackPlayedHandler(IITTrack track);
        public event NewTrackPlayedHandler NewTrackPlayed;
        public event ItunesInitilizedHandler ItunesInitilized;
        private bool _isItunesInitialized;
        private iTunesAppClass _iTunes;

        public int PlayerPosition 
        {
            get
            {
                if (_iTunes.CurrentTrack != null)
                    return _iTunes.PlayerPosition;
                else
                    return 0;
            }
            set
            {
                if (_iTunes.CurrentTrack != null)
                    _iTunes.PlayerPosition = value;
            }
        }

        public IITPlaylist CurrentPlaylist
        {
            get
            {
                VerifyItunesAvailability();
                return _iTunes.CurrentPlaylist;
            }
        }

        public bool Mute 
        {
            get
            { return _iTunes.Mute; }
            set
            {
                _iTunes.Mute = value;
            }
        }

        public ITPlayerState PlayerState 
        {
            get
            {
                try
                {
                    return _iTunes.PlayerState;
                }
                catch
                {
                    return ITPlayerState.ITPlayerStateStopped;
                }
            }
        }

        public int Volume
        {
            get
            {
                return _iTunes.SoundVolume;
            }
            set
            {
                _iTunes.SoundVolume = value;
            }
        }

        public iTunesAPI()
        {
            
        }

        public void InitializeItunesAsync(bool isItunesVisible)
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (sender, e) =>
                {
                    try
                    {
                        iTunesAppClass iTunes = new iTunesAppClass();                        
                        if (!Convert.ToBoolean(e.Argument))
                        {
                            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("itunes");
                            foreach (System.Diagnostics.Process proc in procs)
                            {
                                if (proc.MainWindowTitle == "iTunes")
                                    User32.ShowWindow(proc.MainWindowHandle, User32.WindowStatus.SW_HIDE);
                            }
                        }
                        e.Result = iTunes;
                        _iTunes = iTunes;
                        _iTunes.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(ITunes_OnPlayerPlayEvent);
                        _isItunesInitialized = true;
                    }
                    catch { }
                };

            bw.RunWorkerCompleted += (sender, e) =>
                {
                    if(ItunesInitilized!=null)
                        ItunesInitilized(e.Result as iTunesAppClass);                    
                };
            bw.RunWorkerAsync(isItunesVisible);
        }

        void ITunes_OnPlayerPlayEvent(object iTrack)
        {
            if (NewTrackPlayed != null)
                NewTrackPlayed(iTrack as IITTrack);
        }

        public List<IITPlaylist> GetAllPlaylists()
        {
            VerifyItunesAvailability();
            List<IITPlaylist> playlists = new List<IITPlaylist>();
            foreach (IITPlaylist playlist in _iTunes.LibrarySource.Playlists)
            {
                playlists.Add(playlist);
            }
            return playlists;
        }

        public void Play()
        {
            VerifyItunesAvailability();
            _iTunes.Play();
        }

        public void PlayPause()
        {
            VerifyItunesAvailability();
            _iTunes.PlayPause();
        }

        public void Pause()
        {
            VerifyItunesAvailability();
            _iTunes.Pause();
        }

        public void Stop()
        {
            VerifyItunesAvailability();
            _iTunes.Stop();
        }

        public void Resume()
        {
            VerifyItunesAvailability();
            _iTunes.Resume();
        }

        public void Rewind()
        {
            VerifyItunesAvailability();
            _iTunes.Rewind();
        }

        public void Quit()
        {
            VerifyItunesAvailability();
            _iTunes.Quit();
        }

        public void PreviousTrack()
        {
            VerifyItunesAvailability();
            _iTunes.PreviousTrack();
        }

        public void NextTrack()
        {
            VerifyItunesAvailability();
            _iTunes.NextTrack();
        }

        public void FastForward()
        {
            VerifyItunesAvailability();
            _iTunes.FastForward();            
        }

        public IITTrack GetCurrentTrack()
        {
            VerifyItunesAvailability();            
            return _iTunes.CurrentTrack;
        }

        private void VerifyItunesAvailability()
        {
            if (!_isItunesInitialized)
                throw new Exception("iTunes is not initilized.");
        }
    }
}
